<?php    

/**
 *	Dory.Json.php
 *
 *	JSON Based wrapper for all core Dory Classes @inline{@link{Dory.php}} 
 *	@author Ashwanth Kumar <chronosaeon@gmail.com>
 *	@version Alpha
 */	

class CategoryJSON {
	protected $ID;
	public $Name;
    function __construct($cat)
    {
        $this -> ID = $cat -> ID;
        $this -> Name = $cat -> Name;
    }
}

class CommentsJSON {
	protected $ID;
	public $Poster,$PosterEmail,$Comment,$PostId;
    function __construct($cmt)
    {
		$this -> ID = $cmt -> ID;
		$this -> Poster = $cmt -> Poster;
		$this -> PosterEmail = $cmt -> PosterEmail;
		$this -> Comment = $cmt -> Comment;
		$this -> PostId = $cmt -> PostId;
    }
}


class PostsJSON {
	public $ID,$Author,$File,$Created,$UserRole,$Modified,$isPublished,$ViewCount,$Title,$Comments = array();
    function __construct($post,$comments)
    {
    		$this -> ID = $post -> ID;
    		$this -> Author = $post -> Author;
    		$this -> File = $post -> File;
    		$this -> Created = $post -> Created;
    		//$this -> UserRole = $post -> UserRole;
		$urs = $post -> Find('UserRole',array("ID"=>$post->UserRole),array());
		if(sizeof($urs) > 0) {
			$ur = new UserRoleJSON($urs[0]);
		   	$this -> UserRole = $ur;
		} else { // This case can't happen. If it does, there seems to be some sought of error with the UserRole table data. End gracefully
		 	// TODO: Add a better way of error handling
		   	die('There seems to be an error in the system. Please come back soon, and we shall\'ve it fixed!');
		}
		
		// Adding all the comments to it
		foreach($comments as $c) $this -> Comments[] = $c;
		
    		$this -> Modified = $post -> Modified;
    		$this -> isPublished = $post -> isPublished;
    		$this -> ViewCount = $post -> ViewCount;
    		$this -> Title = $post -> Title;
    }
}

class UsersJSON {
	protected $ID;
	public $Email,$UserRole,$Username, $AuthSession;
    function __construct($user)
    {
        $this -> ID = $user -> ID;
        $this -> Email = $user -> Email;
        //$this -> UserRole = $user -> UserRole;
        $urs = $user -> Find('UserRole',array("ID"=>$user->UserRole),array());
        if(sizeof($urs) > 0) {
        	$ur = new UserRoleJSON($urs[0]);
        	$this -> UserRole = $ur;
        } else { // This case can't happen. If it does, there seems to be some sought of error with the UserRole table data. End gracefully
        	// TODO: Add a better way of error handling
        	die('There seems to be an error in the system. Please come back soon, and we shall\'ve it fixed!');
        	// Possibly send an administrative alert
        }
        $this -> Username = $user -> Username;
    }
}

class UserRoleJSON {
	protected $ID;
	public $Role,$Weight;
    function __construct($urole)
    {
    		$this -> ID = $urole -> ID;
    		$this -> Role = $urole -> Role;
    		$this -> Weight = $urole -> Weight;
    }
}

class UserURLsJSON {
	protected $ID;
	public $URL,$User;
    function __construct($url)
    {
    		$this -> ID = $url -> ID;
    		$this -> URL = $url -> URL;
    		$this -> User = $url -> User;
    }
}

?>
